﻿namespace Chameleon.Data
{
	using System.Linq;

	/// <summary>
	/// Base class from which repositories can derive to implement the IDepository interface.
	/// </summary>
	
	public abstract class Depository : IDepository
	{
		#region IDepository

			/// <summary>
			/// Returns an interface for querying items of a specified type within the data context.
			/// </summary>
			/// <typeparam name="TEntity">Type of the items to query.</typeparam>
		
			protected internal abstract IQueryable<TEntity> Query<TEntity>() where TEntity: class;


			/// <summary>
			/// Inserts an item into the underlying data context and returns a reference to the item.
			/// </summary>
			/// <typeparam name="TEntity">Type of the item to insert into the data context.</typeparam>
			/// <param name="item">Item to insert into the data context.</param>

			public abstract TEntity Insert<TEntity>( TEntity item ) where TEntity: class;
		 

			/// <summary>
			/// Attaches a previously disconnected item to the context.
			/// </summary>
			/// <typeparam name="TEntity">Type of the item to attach to the context.</typeparam>
			/// <param name="item">Item to attach to the data context.</param>

			public abstract void Attach<TEntity>( TEntity item ) where TEntity: class;


			/// <summary>
			/// Deletes an item from the underlying data context.
			/// <para>This will not take effect until a commit is performed.</para>
			/// </summary>
			/// <typeparam name="TEntity">Type of the item to delete from the data context.</typeparam>
			/// <param name="item"></param>

			public abstract void Delete<TEntity>( TEntity item ) where TEntity: class;


			/// <summary>
			/// Commits any outstanding changes to the underlying data context.
			/// </summary>

			public abstract void Submit();

		#endregion


		#region IDisposable

			/// <summary>
			/// When overridden in a derived type, performs tasks associated with freeing, releasing, or resetting unmanaged resources.
			/// </summary>
			/// <param name="disposing">When true, releases unmanaged resources being used by managed components.</param>
			
			protected abstract void Dispose( bool disposing );


			/// <summary>
			/// Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
			/// </summary>

			public void Dispose()
			{
				Dispose( true );
			}

		#endregion
	}
}
